(function ($) {
	$.fn.tree = {
		html_str:'',
		select_obj:{
			co:{
				areaName:'中国',
				parentId:0,
				areaId:2,
				level:0
			},
			pr:{
				areaName:'',
				parentId:0,
				areaId:0,
				level:0
			},
			ci:{
				areaName:'',
				parentId:0,
				areaId:0,
				level:0
			},
			di:{
				areaName:'',
				parentId:0,
				areaId:0,
				level:0
			}
		},
		obj_tu:{},
		tmpid:0,
		init:function(setting){
			if(typeof(setting.nodes)=="string"){
				setting.nodes=$.parseJSON(setting.nodes);
			}
			$.each(setting.nodes,function(i,v){
				if(v.parentId==0){
					$.fn.tree.html_str='';
					$.fn.tree.html_str +='<div class="z_country z_address1"><ul class="nation"><li><span></span><input type="checkbox" '+(v.isChecked=='true'?"checked":"")+' name="tl_sel" level="0" value=\''+JSON.stringify(v)+'\' onclick=\'javascript:$.fn.tree.itemclick('+JSON.stringify(v)+',0);\'/><p>'+v.areaName+'</p></li></ul></div>';
					if(v.nodes!='' && v.nodes!=undefined){
						$.fn.tree.html_str +='<div class="z_provice z_gone">';
						$.each(v.nodes,function(ii,vv){
							$.fn.tree.html_str +='<div class="z_country z_address2 "><ul class="z_dizhi2"><li><span></span><input type="checkbox" '+(vv.isChecked=='true'?'checked':'')+' name="tl_sel" level="1" value=\''+JSON.stringify(vv)+'\' onclick=\'javascript:$.fn.tree.itemclick('+JSON.stringify(vv)+',1);\'/><p>'+vv.areaName+'</p></li></ul>';
							if(vv.nodes!='' && vv.nodes!=undefined){
								$.fn.tree.html_str +='<div class="z_address3 z_gone">';
								$.each(vv.nodes,function(iii,vvv){
									$.fn.tree.html_str +='<div class="z_country"><ul class="city"><li><span></span><input type="checkbox" '+(vvv.isChecked=='true'?'checked':'')+' name="tl_sel" level="2" value=\''+JSON.stringify(vvv)+'\' onclick=\'javascript:$.fn.tree.itemclick('+JSON.stringify(vvv)+',2);\'/><p>'+vvv.areaName+'</p></li></ul>';
									if(vvv.nodes!='' && vvv.nodes!=undefined){
										$.fn.tree.html_str +='<div class="z_country z_address4 ">';
										$.each(vvv.nodes,function(ii,vvvv){
											$.fn.tree.html_str +='<div class="z_country"><ul class="county"><li><input type="checkbox" '+(vvvv.isChecked=='true'?'checked':'')+' name="tl_sel" level="3" value=\''+JSON.stringify(vvvv)+'\' onclick=\'javascript:$.fn.tree.itemclick('+JSON.stringify(vvvv)+',3);\'/><p>'+vvvv.areaName+'</p></li></ul></div>';
										});
										$.fn.tree.html_str +='</div>';
									}
									$.fn.tree.html_str +='</div>';
								});
								$.fn.tree.html_str +='</div>';
							}
							$.fn.tree.html_str +='</div>';
						});
						$.fn.tree.html_str +='</div>';
					}
				}
			});
			$('.z_zone').html($.fn.tree.html_str);
			$.fn.tree.run();
		},
		run:function(){
			$(".g_houLeft").hover(function(){
				$(".g_niazhanK").removeClass("g_none");
				$(this).css({"background":"#fff","border":"1px solid #ddd","width":"178px","height":"70px"});
				$(this).children("span").css({"color":"#222","border":"none","background":"url('images/g_huiXia.png') 154px 11px no-repeat"});
			},function(){
				$(".g_niazhanK").addClass("g_none");
				$(this).css({"background":"#1777d8","border":"none","width":"180px","height":"72px"});
				$(this).children("span").css({"color":"#fff","border-right":"1px solid #4592e0","background":"url('images/g_upDown.png') 154px 11px no-repeat"});
			})
			$(".g_huMing").hover(function(){
				$(".g_yuiChu").removeClass("g_none");
				$(this).css({"background":"#fff"});
				$(this).children("span").css({"color":"#222","background":"url('images/g_huiXia.png') 50px 11px no-repeat"});
			},function(){
				$(".g_yuiChu").addClass("g_none");
				$(this).css({"background":"#1777d8"});
				$(this).children("span").css({"color":"#fff","background":"url('images/g_upDown.png') 50px 11px no-repeat"});
			})
			$(".g_bifUlbao li").hover(function(){
				$(this).addClass("g_baiSd");
			},function(){
				$(this).removeClass("g_baiSd");
			})
			$(".g_smallUlbao li").hover(function(){
				$(this).css("background","#fff");
			},function(){
				$(this).css("background","#fff");
			})
			$(".g_nCaidan").hover(function(){
				$(".g_biaoGecon").removeClass("g_none");
				$(this).children("span").css({"color":"#333","background":"url('images/g_huiGong.png') 0px 2px no-repeat"});
			},function(){
				$(".g_biaoGecon").addClass("g_none");
				$(this).children("span").css("color","#fff");
				$(this).children("span").css({"color":"#fff","background":"url('images/g_Sheiji.png') 0px 2px no-repeat"});
			})
			$(".g_smallUlbao li a").hover(function(){
				$(this).css("background","#fff");
				$(this).addClass("g_Ahover");
			},function(){
				$(this).css("background","none");
				$(this).removeClass("g_Ahover");
			})
			$(".g_smallUlbao li a").click(function(){
				$(this).addClass("g_danQia").parent().siblings().children("a").removeClass("g_danQia");
			})

//        /*左侧点击效果*/
//        $(".g_indent p").css("top","1px");
//        $(".g_indent p").click(function(event) {
//            var h = $(this).get(0).style.top;
//            if(h =="1px"){
//                $(this).removeClass('g_below').addClass('g_leftUp');
//                //$(this).removeClass('g_leftUp').addClass('g_below');
//                //$(this).parent().siblings().find('.g_dowmXia').slideUp(800).siblings('p').removeClass('g_below').addClass('g_leftUp');
//                $(this).parent().siblings().find('.g_dowmXia').slideUp(800).siblings('p').removeClass('g_leftUp').addClass('g_below');
//                $(this).css("top","2px");
//                if($(this).siblings('.g_dowmXia').find('li').length>0){
//                    $(this).siblings('.g_dowmXia').slideDown(800);
//                }
//                $(this).css("top","2px").parent().siblings().children('p').css("top","1px");
//            }
//            else{
//                $(this).removeClass('g_leftUp').addClass('g_below');
//                //$(this).removeClass('g_below').addClass('g_leftUp');
//                $(this).siblings('.g_dowmXia').slideUp(800);
//                $(this).css("top","1px");
//            }
//        });
			/*点击国家一级*/

			$(".z_address1 span").click(function(){
				var a=$(this).css("top");
				if(a=="1px")
				{
					$(this).addClass('z_minus');
					$(".z_provice").removeClass('z_gone');
					$(this).css("top","2px");
				}
				else
				{
					$(this).removeClass('z_minus');
					$(".z_provice,.z_address3,z_address4").addClass('z_gone');
					$(".z_dizhi2 span").removeClass('z_minus').css("top","3px");
					$(this).css("top","1px");
					$(".z_address3 span").removeClass('z_minus').css("top","5px");
				}
			})
			/*点击省份二级*/
			$(".z_dizhi2 span").click(function(){
				if(true==$($(this).siblings("input")).prop("disabled")){
					for(var i=0;i<$(this).parent().parent().siblings(".z_address3").find("input").length;++i){
						$($(this).parent().parent().siblings(".z_address3").find("input")[i]).prop("disabled",true);
					}
				}
				var b=$(this).css("top");
				if(b=="3px")
				{
					$(this).addClass('z_minus');
					$(this).parent().parent().siblings(".z_address3").removeClass('z_gone').css("top","5px");
					$(this).css("top","4px");
					$(".z_address4").addClass('z_gone');
				}
				else
				{
					$(this).removeClass('z_minus');
					$(this).parent().parent().siblings(".z_address3").addClass('z_gone');
					$(this).parent().parent().siblings(".z_address3").find("span").removeClass('z_minus').css("top","5px");
					$(this).parent().parent().siblings(".z_address3").find('.z_address4').addClass('z_gone');
					$(this).css("top","3px");
				}
			})
			/*点击省份三级*/
			$(".z_address3 span").click(function(){
				var c=$(this).css("top");
				if(c=="5px")
				{
					$(this).addClass('z_minus');
					$(this).parent().parent().siblings(".z_address4").removeClass('z_gone');
					$(this).css("top","6px");
				}
				else
				{
					$(this).removeClass('z_minus');
					$(this).parent().parent().siblings(".z_address4").addClass('z_gone');
					$(this).css("top","5px");
				}
			})

			/*点击国家复选框*/
//      $(".z_address1 input").click(function(){
//         if(this.checked==true)
//         {
//            for(var i=0;i<$(".z_provice input").length;i++)
//            {
////               $(".z_provice input")[i].checked=true;
////            	$($(".z_provice input[level=1]")[i]).prop("disabled",true);
//            }
//         }
//         else
//         {
//            for(var i=0;i<$(".z_provice input").length;i++)
//            {
////               $(".z_provice input")[i].checked=false;
////            	$(".z_provice input[level=1]")[i].prop("disabled",false);
//            }
//         }
//       });
			/*点击省份复选框*/
			$(".z_dizhi2 input[level=1]").click(function(event) {
				//所有省集合
				var provinceLi=$(".z_provice input");
				if(this.checked==true)
				{
					for(var j=0;j<$(this).parent().parent().siblings(".z_address3").find("input").length;j++)
					{
						$(this).parent().parent().siblings(".z_address3").find("input")[j].checked=true;
					}

					//如果所有省都已勾选，则全国也勾选
					for(var j=0;j<provinceLi.length;++j){
						if(false==provinceLi[j].checked){
							return;
						}
					}

					$(".z_address1 input")[0].checked=true;
				}
				else
				{
					//全国取消勾选
					$(".z_address1 input")[0].checked=false;
					for(var j=0;j<$(this).parent().parent().siblings(".z_address3").find("input").length;j++)
					{
						$(this).parent().parent().siblings(".z_address3").find("input")[j].checked=false;
					}

				}
			});
			/*点击市复选框*/
			$(".z_address3 .z_country input[level=2]").click(function(event) {
				//当前所选市所在省
				var province=$(this).parent().parent().parent().parent().siblings(".z_dizhi2").find("input")[0];
				//当前所选市所在省其下市的集合
				var cityLi=$(this).parent().parent().parent().parent().find("input[level=2]");
				//当前所在市其下所有县
				var countyLi=$(this).parent().parent().siblings(".z_address4").find("input");
				if(this.checked==true)
				{
					for(var j=0;j<countyLi.length;j++)
					{
						countyLi[j].checked=true;
					}

					//所有市都勾选，那么省也勾选
					for(var j=0;j<cityLi.length;++j){
						if(false==cityLi[j].checked){
							return;
						}
					}

					province.checked=true;

				}
				else
				{
					//全国取消勾选
					$(".z_address1 input")[0].checked=false;
					//取消市，父级省取消勾选
					province.checked=false;
					for(var j=0;j<countyLi.length;j++)
					{
						countyLi[j].checked=false;
					}

				}
			});

			/*点击县复选框*/
			$(".z_address4 .z_country input[level=3]").click(function(){
				//当前所选县所在省
				var province=$(this).parent().parent().parent().parent().parent().parent().siblings(".z_dizhi2").find("input")[0];
				//当前所选县所在市
				var city=$(this).parent().parent().parent().parent().siblings("ul").find("input")[0];
				//当前所选县所在省其下所有市集合
				var cityLi=$(this).parent().parent().parent().parent().parent().parent().find("input[level=2]");
				//当前所选县所在市其下所有县集合
				var countyLi=$(this).parent().parent().parent().parent().find("input[level=3]");
				if(this.checked==false){
					//全国取消勾选
					$(".z_address1 input")[0].checked=false;
					//取消县，父级市,省取消勾选
					city.checked=false;
					province.checked=false;
				}else{
					//如果所有市下所有县都已勾选，那么市也勾选
					for(var j=0;j<countyLi.length;++j){
						if(false==countyLi[j].checked){
							return;
						}
					}
					//勾选市
					city.checked=true;
					//如果省下所有市都被勾选，那么省也勾选
					for(var j=0;j<cityLi.length;++j){
						if(false==cityLi[j].checked){
							return;
						}
					}
					province.checked=true;
				}

			});


			$(".z_address1 span").trigger("click");
//      $("input[type=checkbox]:checked").siblings("span").trigger("click");
			/*$(".z_dizhi2 span").trigger("click");
			 $(".z_address3 span").trigger("click");*/

		},
		itemclick:function(data,level){
			switch(level){
				case 0:
					if(data.isChecked=='true'){
						$.fn.tree.select_obj.co.areaName=data.areaName;
						$.fn.tree.select_obj.co.parentId=data.parentId;
						$.fn.tree.select_obj.co.areaId=data.areaId;
						$.fn.tree.select_obj.co.level=data.level;
					}
					break;
				case 1:
					if(data.isChecked=='true'){
						$.fn.tree.select_obj.pr.areaName=data.areaName;
						$.fn.tree.select_obj.pr.parentId=data.parentId;
						$.fn.tree.select_obj.pr.areaId=data.areaId;
						$.fn.tree.select_obj.pr.level=data.level;
					}
					break;
				case 2:
					if(data.isChecked=='true'){
						$.fn.tree.select_obj.ci.areaName=data.areaName;
						$.fn.tree.select_obj.ci.parentId=data.parentId;
						$.fn.tree.select_obj.ci.areaId=data.areaId;
						$.fn.tree.select_obj.ci.level=data.level;
					}

					break;
				case 3:
					if(data.isChecked=='true'){
						$.fn.tree.select_obj.di.areaName=data.areaName;
						$.fn.tree.select_obj.di.parentId=data.parentId;
						$.fn.tree.select_obj.di.areaId=data.areaId;
						$.fn.tree.select_obj.di.level=data.level;
					}

					break;
			}
			$.fn.tree.obj_tu=data;
		},
		getSelect:function(ajs){
			$('input:checkbox[name=tl_sel]:checked').each(function(i,v){
				var level=$(v).attr('level');
				var data=$.parseJSON($(v).attr('value'));
				if(ajs!=undefined && ajs!=''){
					$.fn.tree.tmpid=data;
					return $.fn.tree.tmpid;
				}else{
					$.fn.tree.tmpid=data.areaId;
					return $.fn.tree.tmpid;
				}

				/*switch(level){
				 case '0':
				 $.fn.tree.select_obj.co.areaName=data.areaName;
				 $.fn.tree.select_obj.co.parentId=data.parentId;
				 $.fn.tree.select_obj.co.areaId=data.areaId;
				 $.fn.tree.select_obj.co.level=data.level;
				 return data.areaId;
				 break;
				 case '1':
				 $.fn.tree.select_obj.pr.areaName=data.areaName;
				 $.fn.tree.select_obj.pr.parentId=data.parentId;
				 $.fn.tree.select_obj.pr.areaId=data.areaId;
				 $.fn.tree.select_obj.pr.level=data.level;
				 return data.areaId;
				 break;
				 case '2':
				 $.fn.tree.select_obj.ci.areaName=data.areaName;
				 $.fn.tree.select_obj.ci.parentId=data.parentId;
				 $.fn.tree.select_obj.ci.areaId=data.areaId;
				 $.fn.tree.select_obj.ci.level=data.level;
				 alert(data.areaId);
				 return data.areaId;

				 break;
				 case '3':
				 $.fn.tree.select_obj.di.areaName=data.areaName;
				 $.fn.tree.select_obj.di.parentId=data.parentId;
				 $.fn.tree.select_obj.di.areaId=data.areaId;
				 $.fn.tree.select_obj.di.level=data.level;
				 return data.areaId;

				 break;
				 }*/
			});
			//return $.fn.tree.select_obj;
		}
	};
})(jQuery)